using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Stock.Dto;
using JTZY.Model.Stock;
using JTZY.Repository;
using JTZY.Service.Stock.IStockService;
using JTZY.Model.Product;
using JTZY.Model.Goods;
using JTZY.Model.Brand;

namespace JTZY.Service.Stock
{
    /// <summary>
    /// 调仓订单货物明细Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ITransferorderdetailService), ServiceLifetime = LifeTime.Transient)]
    public class TransferorderdetailService : BaseService<Transferorderdetail>, ITransferorderdetailService
    {
        /// <summary>
        /// 查询调仓订单货物明细列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public List<TransferorderdetailDto> GetList(TransferorderdetailQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .LeftJoin<Prodinfo>((it, prodinfo) => it.SnCode == prodinfo.SnCode)
                .LeftJoin<Corecmsgoods>((it, prodinfo,goods) => prodinfo.GoodsId == goods.Id)
                .LeftJoin<Corecmsbrand>((it, prodinfo, goods,brand) => goods.BrandId == brand.Id)
                .Where(predicate.ToExpression())
                .Select((it, prodinfo, goods, brand) => new TransferorderdetailDto()
                {
                    Id = it.Id,
                    OrderId = it.OrderId,
                    ProductId = it.ProductId,
                    SnCode = it.SnCode,
                    BrandName = brand.Name,
                    ProductDate= prodinfo.ProdctDate,
                })
                .ToList();

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Transferorderdetail GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加调仓订单货物明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Transferorderdetail AddTransferorderdetail(Transferorderdetail model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改调仓订单货物明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateTransferorderdetail(Transferorderdetail model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Transferorderdetail> QueryExp(TransferorderdetailQueryDto parm)
        {
            var predicate = Expressionable.Create<Transferorderdetail>();
            predicate= predicate.AndIF(!string.IsNullOrEmpty(parm.OrderId),it=>it.OrderId==parm.OrderId);
            return predicate;
        }
    }
}